home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3213 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.1 KB

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q]Assigning function pointer in C/C++.
  5. Date: 22 Jan 1996 19:22:29 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4e0o5l$fjc@colossus.holonet.net>
  8. References: <DL3JJu.5nB.0.queen@torfree.net> <4doc42$gsb@bmdhh222.bnr.ca> <ALUN.CHAMPION.96Jan19113523@g7240065.bridge.bst.bls.com> <4e09re$kit@populus.slu.se>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Thomas Johansson (Thomas.Johansson@stax.slu.se) wrote:
  13.  
  14. : Is there any way that you can declare a variable to hold the resulting
  15. : pointer,
  16. : or must it be 'used' right away ?
  17.  
  18. Yes, there's a way -- that's exactly what we're talking about here.
  19. You define a pointer, initialize it (perhaps at the same time, perhaps
  20. later), and then use it any time thereafter.
  21.  
  22. : something like
  23.  
  24. :   typdef int (A::*p)(void) Fp;     // Fp is a type... 
  25.  
  26. You made the mistake of putting two identifiers in your
  27. typedef; the name of the type above is the "p" even though
  28. it's in parentheses.  The Fp is extra and causes an error.
  29. Correct:
  30.     typedef int (A::*Fp)(void);
  31.  
  32. BTW, you don't *have* to use a typedef.
  33.  
  34. :   Fp f = (a.*p);       // the object a (of class A) has a member p
  35.  
  36. Should be
  37.     Fp f = &A::p;   // where  int p(void) is a member-function of A
  38.  
  39. :   int z = f();
  40.  
  41. Should be
  42.     int z = (a.*f)();   // note parens, .* has low precedence
  43.  
  44. The operators .* and ->* are used to call the function, not to
  45. initialize the pointer.
  46.  
  47. Earlier,
  48. Scott J. McCaughrin (sjmccaug@prairienet.org) wrote:
  49. : >bh332@freenet.toronto.on.ca (Karim Ladha) wrote:
  50. : >>
  51. : >>How is it possible to assign a declared variable in C++ a pointer to
  52. : >>some function member? If you know of a solution, post. Greatly appreciated.
  53. : >>
  54.  [snip]
  55.  
  56. :  As usual, the C++ crowd has to bury us... 
  57. :  ... The author didn't ask about 'member' functions ...
  58.  
  59. Ummm, in general I agree with your rant, but in this specific
  60. case the original author DID ask about member functions.  You
  61. quoted it, in fact!
  62. --
  63. Russell Blackadar,   russell@mdli.com
  64.